package com.mdinfo.base.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by admin on 2017/6/21.
 */
public class StringUtil {

    /**
     * 用","分割的字符串转换成List<String>
     */
    public static List<String> string2List(String str){
        List<String> list = new ArrayList<>();
        if(str!=null && str.trim().length()>0){
            String[] strs = str.split(",");
            for(String s : strs){
                list.add(s);
            }
        }
        return list;
    }

    /**
     * List<String>转换成用","分割的字符串
     */
    public static String list2String(List<String> list){
        String result = "";
        if(list!=null && list.size()>0){
            for(String str : list){
                result = result + "," + str;
            }
            result = result.substring(1);
        }
        return result;
    }

    /**
     * List<Integer>转换成","分割的字符串
     */
    public static String listInt2String(List<Integer> list){
        String result = "";
        if(list!=null && list.size()>0){
            for(Integer i : list){
                result = result + "," + String.valueOf(i);
            }
            result = result.substring(1);
        }
        return result;
    }

    /**
     * 用","分割的字符串转换成List<String>
     */
    public static List<Integer> string2ListInt(String str){
        List<Integer> list = new ArrayList<>();
        if(str!=null && str.trim().length()>0){
            String[] strings = str.split(",");
            for(String s : strings){
                list.add(Integer.parseInt(s));
            }
        }
        return list;
    }

    /**
     * 判断字符串是否为空
     */
    public static boolean isEmpty(String str){
        if(str==null){
            return true;
        }
        if(str.trim().length()==0){
            return true;
        }
        return false;
    }

    /**
     * 获取毫秒数+6位随机数字
     */
    public static String randomName(){
        long time = new Date().getTime();
        String result = String.valueOf(time);

        Random random = new Random();
        for(int i=0;i<6;i++){
            int randomInt = random.nextInt(10);
            result = result + String.valueOf(randomInt);
        }
        return result;
    }

    /**
     * 获取6位随机数字
     */
    public static String random6(){

        String result = "";
        Random random = new Random();
        for(int i=0;i<6;i++){
            int randomInt = random.nextInt(10);
            result = result + String.valueOf(randomInt);
        }
        return result;
    }

    /**
     * 获取uuid
     */
    public static String getUUid(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-","");
    }

}
